package com.weiyi.controller.biz;

import com.weiyi.biz.dto.EquipInfo;
import com.weiyi.biz.dto.WarnCot;
import com.weiyi.biz.dto.WyCountInfo;
import com.weiyi.biz.dto.WyLogInfo;
import com.weiyi.biz.mapper.WyManageMapper;
import com.weiyi.common.core.controller.BaseController;
import com.weiyi.controller.biz.dto.EquipTree;
import com.weiyi.framework.util.ShiroUtils;
import com.weiyi.system.domain.SysDictData;
import com.weiyi.system.domain.SysRole;
import com.weiyi.system.domain.SysUser;
import com.weiyi.system.service.ISysDictDataService;
import com.weiyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 首页展示
 *
 * @author gq
 * @date 2020-08-31
 */
@Controller
@RequestMapping("/main/wyInfo")
public class WyManageController extends BaseController {
    private String prefix = "main/wyInfo";

    @Autowired
    private WyManageMapper wyManageMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDictDataService sysDictDataService;

    @PostMapping("/wylist")
    @ResponseBody
    public List<EquipTree> log() {

        List<EquipTree> trees = new ArrayList<>();

        SysUser loginUser = ShiroUtils.getSysUser();

        if (wyManageMapper.isAdmin(loginUser.getUserId())>0) {
            //超级管理员查看所有传感器信息
            List<Long> ids = wyManageMapper.selectDistinctUser();
            if (null != ids && !ids.isEmpty()) {

                List<EquipInfo> list = null;
                EquipTree tree = null;

                for (Long id : ids) {
                    String name = wyManageMapper.selectUserById(id);
                    list = wyManageMapper.selectWyEquipByUserId(id);

                    tree = new EquipTree();
                    tree.setId(id);
                    tree.setName(name);
                    tree.setList(list);
                    trees.add(tree);

                }
            }

        } else {
            //查看名下的传感器信息
            List<EquipInfo> equips = wyManageMapper.selectWyEquipByUserId(loginUser.getUserId());
            EquipTree tree = new EquipTree();
            tree.setId(loginUser.getUserId());
            tree.setName(loginUser.getLoginName());
            tree.setList(equips);
            trees.add(tree);

        }

        return trees;
    }


    @GetMapping("/logList")
    @ResponseBody
    public List<WyLogInfo> logList(@RequestParam("id") Long equipId,@RequestParam(value = "startTime",required = false) String startTime,@RequestParam(value = "endTime",required = false) String endTime) {
            return wyManageMapper.selectWyLogList(equipId, startTime, endTime);

    }

    @GetMapping("/count")
    @ResponseBody
    public WyCountInfo count(@RequestParam("id") Long id, @RequestParam(value = "startTime",required = false) String startTime, @RequestParam(value = "endTime",required = false) String endTime) {
        WyCountInfo wyCountInfo = wyManageMapper.selectCount(id, startTime, endTime);
        Long max = wyCountInfo.getMax();
        Long min = wyCountInfo.getMin();
        Long ed = max - min;
        wyCountInfo.setEd(ed);
        return wyCountInfo;
    }


    @GetMapping("/warnCot")
    @ResponseBody
    public WarnCot warnCot(@RequestParam("id") Long id, @RequestParam(value = "startTime",required = false) String startTime, @RequestParam(value = "endTime",required = false) String endTime) {

        //偏移预警值
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("weiyi_equip");
        List<SysDictData> list = sysDictDataService.selectDictDataList(sysDictData);
        if (list != null && !list.isEmpty() && list.size() == 1) {
            Map<String,String > param = new HashMap<>();
            param.put("id", id.toString());
            param.put("val", Long.valueOf(list.get(0).getDictValue()).toString());
            param.put("startTime", startTime);
            param.put("endTime", endTime);
            WarnCot warnCot = wyManageMapper.selectWarnCot(param);
            WarnCot cot = wyManageMapper.selectDirection(id);
            warnCot.setName1(cot.getName1());
            warnCot.setName2(cot.getName2());
            return warnCot;
        }
        return null;
    }

}
